/*
** COMTEST.C: Test the COM port library using loopback.
*/

#include <pictor.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <comlib.h>
//#include <mouse.h>
#include <string.h>

COLORSTRUCT msgcolors = {
   foreback(BLACK,CYAN),
   foreback(BOLD|WHITE,CYAN),
   foreback(WHITE,BLACK),
   foreback(BOLD|WHITE,BLACK)
};

VIDEOSTRUCT vcfg;

int main(int argc, char * argv[])
{
	char buffer[256];
	char test_string_n[] = "The quick brown fox jumps over the lazy dog.\n0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char test_string_r[] = "The quick brown fox jumps over the lazy dog.\r";
	
	int  error_code, com_no, count, timeout;
	char com_port[] = "COMX";
	
	if(argc < 2)
	{
		com_no = CO_COM1;
	}
	else
	{
		if(!strncmp(com_port, argv[1], 3)) //strncmp returns 0 on success.
		{
			com_no = atoi(&argv[1][3]) - 1;
		}
	/*	else if(argv[1][0]
		{
			
		} */
		else
		{
			printf("Invalid COM port specified.");
			return EXIT_FAILURE;
		}
	}
	
	/* while(count < 30)
	{
		vputc(test_string_r[count]);
		count++;
	}
	count = 0; */
		
	initvideo();
	cls();
	hookints(&msgcolors);
	getvconfig(&vcfg);
	sprintf(buffer, "Is a loopback adapter attached to COM%i", com_no + 1); 
	if(messagebox(buffer, "Question",MB_YESNO,&msgcolors))
	{
	   if(comopen(com_no, CO_BAUD300, CO_NOPARITY, CO_DATA8, CO_STOP1, CO_IRQDEFAULT))
	   {
		  beep();
		  sprintf(buffer, "COM%i was not properly initialized (possibly not\ndetected in the BIOS Data Area).", com_no + 1); 
		  messagebox(buffer, "Error", MB_OK, &msgcolors);
		  error_code = EXIT_FAILURE; 
	   }
	   else
	   {
		   wopen(center(10, vcfg.rows), center(50, vcfg.columns), 10, 50, WHITE, WO_TEXTCURSOR | WO_TEXTWRAP);
		   wtitle("Baud Rate Tests");
		   computs(test_string_r);
		   comgets(buffer, 128);
		   wputs(buffer);
		   
		   if(!strcmp(buffer, test_string_r)) //strcmp returns 0 on success.
		   {
			   wputs("COM port successfully tested.\nPress any key to continue...\n");
			   getchar();
			   wclear();
			   
			   comflush();
			   count = 0;
			   
			   /* Essentially a C-style wputs() that illustrates the COM port speed- ttywputs */
			   
			   //computs(test_string_r);
			   do{
				   computc(test_string_r[count]);
				   //if(comloc() > 0)
				   //{

					   comgetc(&buffer[count]);
					   wputc(buffer[count]);
					   count++;
				   //}
				  /* else
				   {
					   /* Normally other logic would go here, and
					    * timeout would be detected in another manner,
					    * such as checking at the end of one application
					    * loop. */
					   /*if(comgetc(&buffer[count]))
					   {
						   wputs("COM port timed out! Press any key to exit...");
						   getchar();
						   break;
					   }
					   else
					   {
						   count++;
					   } 
				   } */
				      
			   }while((count < 128) && (buffer[count] != '\r'));
			   buffer[count + 1] = '\0';
			   count = 0;
			   
			   
		   }
		   else
		   {
			   wputs("\nData was not successfully retrieved.\nPress any key to exit...\n");
			   getchar();
			   error_code = EXIT_FAILURE;
		   }  
		   
	   }
	   
	}
	else
	{
	   beep();
	   messagebox("This test program requires a loopback adapter to run successfully.", 
		"Error", MB_OK, &msgcolors);
		error_code = EXIT_FAILURE;
	}
	
	/* restore screen */
	vcolor(foreback(WHITE,BLACK));
	cls();
	return error_code;
}
